package com.nbjtjc.safe.api.know;

import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.model.know.SafeLabel;
import com.nbjtjc.safe.service.know.SafeLabelService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 知识点的标签，分类
 */
@RestController
@RequestMapping("/label")
@CrossOrigin("*")
public class LabelController {
    @Autowired
    private SafeLabelService safeLabelService;

    @RequestMapping(value = "/add",method = {RequestMethod.POST})
    @SysLog("增加标签")
    public Result add(SafeLabel safeLabel) {
        Result result=new Result();
        try {
            result=safeLabelService.add(safeLabel);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
    @RequestMapping(value = "/update",method = {RequestMethod.POST})
    @SysLog("更新知识库中的标签")
    public Result update(SafeLabel safeLabel) {
        Result result=new Result();
        try {
            result=safeLabelService.update(safeLabel);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
    @RequestMapping(value = "/delete",method = {RequestMethod.POST})
    @SysLog("删除标签")
    public Result delete(@RequestParam("deleteLabelsRoutes") List<String> deleteLabelsRoutes
    ,int labelId) {
        Result result=new Result();
        try {
            result=safeLabelService.delete(deleteLabelsRoutes,labelId);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/find",method = {RequestMethod.POST})
    public Result find(SafeLabel param, Page page) {
        Result result=new Result();
        try {
            result=safeLabelService.find(param,page);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
    @RequestMapping(value = "/findLabelStr",method = {RequestMethod.POST})
    public Result findLabelStr(SafeLabel param, Page page) {
        Result result=new Result();
        try {
            result.setData(safeLabelService.findLabelStr(param,page));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
    @GetMapping("/topLabel")
    public Result topLabel(String label, @RequestParam("searchLabels") ArrayList<String> searchLabels){
        Result result=new Result();
        result=safeLabelService.topLabel(label,searchLabels);
        return result;
    }

    @GetMapping("/inspectLabels")
    public Result inspectLabels(String label, @RequestParam("searchLabels") ArrayList<String> searchLabels){
        Result result=new Result();
        result=safeLabelService.inspectLabel(label,searchLabels);
        return result;
    }

    /**
     * 获取整个标签结构
     * @param companyId
     * @return
     */
    @GetMapping("/getLabelConstruct/{companyId}")
    public Result getLabelConstruct(@PathVariable("companyId") int companyId){
        return Result.success(safeLabelService.getLabelStructure(companyId));
    }

    /**
     * 复制整个标签结构
     * @param fromCompanyId
     * @param toCompanyId
     * @return
     */
    @GetMapping("/copySafeLabelConstruct/{fromCompanyId}/{toCompanyId}")
    public Result copySafeLabelConstruct(@PathVariable("fromCompanyId") int fromCompanyId,@PathVariable("toCompanyId") int toCompanyId){
        return Result.success(safeLabelService.copySafeLabel(fromCompanyId,toCompanyId));
    }

}
